var prefs = new _IG_Prefs();
var reminders = new Array();
var size = 5;
var TYPE = 0;
var EXPLAIN = 1;
var YEAR = 2;
var MONTH = 3;
var DATE = 4;
var one_day=1000 * 60 * 60 * 24;
var one_minute = 1000 * 60;
var year_pattern=/^\d{4}$/;
var month_pattern=/^\d{1,2}$/;
var date_pattern=/^\d{1,2}$/;
			
function init() {	
	load();
	refreshTable();
	setInterval("onTimer()", one_minute); 
}
				
function initTable() {
	for (var i = 0; i < reminders.length; i++) {
		if (i == size) {
			break;
		}
		addRowToTable(reminders[i][TYPE], reminders[i][EXPLAIN], reminders[i][YEAR], 
			reminders[i][MONTH], reminders[i][DATE], i);
	}
}
				
function edit() {
	_gel("edit").style.display="none";
	_gel("hide").style.display="block";
	document.reminderForm.style.display="block";
	_IG_AdjustIFrameHeight();
}
				
function hide() {
	_gel("edit").style.display="block";
	_gel("hide").style.display="none";
	document.reminderForm.style.display="none";
	_IG_AdjustIFrameHeight();
}
			
function add() {
	var type = _gel("type").value;
	var explain = _gel("explain").value;
	var year = _gel("year").value;
	var month = _gel("month").value;
	var date = _gel("date").value;
		
	if (validateDate(year, month, date)) {
		addReminder(type, explain, year, month, date);
	}
}
				
function refreshTable() {
	reminders.sort(sortByReminder);
	clearTable();
	initTable();
	_IG_AdjustIFrameHeight();
}
		
function load() {
	size = prefs.getInt("display_num");
	var remindersstring = prefs.getString("reminder_values");
    if (remindersstring) {
    	reminders = JSON.parse(remindersstring);
    }
}
	
function addReminder(type, explain, year, month, date) {
	var reminder = [type, explain, year, month, date];
	reminders.push(reminder);
	prefs.set("reminder_values", JSON.stringify(reminders));
	refreshTable();
}
				
function deleteReminder(index) {
	reminders.splice(index, 1);
	prefs.set("reminder_values", JSON.stringify(reminders));
	refreshTable();
}
	
function sortByReminder(reminderA, reminderB) {
	var today = new Date();
	var dateA = new Date(reminderA[YEAR], reminderA[MONTH] - 1, reminderA[DATE]);
	var dateB = new Date(reminderB[YEAR], reminderB[MONTH] - 1, reminderB[DATE]);
	return getLeftDays(dateA) - getLeftDays(dateB);
}
	
function clearTable() {
	var tbl = _gel('reminders');
	while (true) {
		if (tbl.rows.length > 1) {
			tbl.deleteRow(tbl.rows.length - 1);
		} else {
			break;
		}
	}
}
				
function addRowToTable(type, explain, year, month, date) {
	var todate = new Date(year, month - 1, date);
	
	var tbl = _gel('reminders');
	var lastRow = tbl.rows.length;
	var row = tbl.insertRow(lastRow);
	if (lastRow % 2 == 0) {
		row.className = "odd";
	}
	  
	var type_cell = row.insertCell(0);
	var type_textnode = document.createTextNode(type);
	type_cell.appendChild(type_textnode);
	  
	var explain_cell = row.insertCell(1);
	var explain_textnode = document.createTextNode(explain);
	explain_cell.appendChild(explain_textnode);
				
	var date_cell = row.insertCell(2);
	var date_textnode = document.createTextNode(month + "-" + date);
	date_cell.appendChild(date_textnode);
					
	var anniversary = 0;
	if (year) {
		anniversary = getAnniversary(todate);
	}
	var anniversary_cell = row.insertCell(3);
	var anniversary_textnode = document.createTextNode(anniversary);
	anniversary_cell.appendChild(anniversary_textnode);
					
	var days = getLeftDays(todate);
	var left_cell = row.insertCell(4);
	var left_textnode = document.createTextNode(days);
	if (days <= 3) {
		left_cell.style.color = "red";
	} else if (days <= 10) {
		left_cell.style.color = "green";
	} 
	left_cell.appendChild(left_textnode);
					
	var delete_cell = row.insertCell(5);
	var delete_href = document.createElement("a");
	delete_href.setAttribute("href", "#");
	delete_href.setAttribute("onclick", "deleteReminder(" + (lastRow - 1) + ")");
	delete_cell.appendChild(delete_href);
	var delete_img = _IG_GetImage("http://gremindme.googlecode.com/svn/trunk/delete.jpg");
	delete_href.appendChild(delete_img);
}
				
function getAnniversary(todate) {
	var today = new Date();
	if (today.getFullYear() < todate.getFullYear()) {
		return 0;
	} else {
		if (today.getMonth() > todate.getMonth() || 
				(today.getMonth() == todate.getMonth() && today.getDate() > todate.getDate())) {
			return today.getFullYear() - todate.getFullYear() + 1;
		} else {
			return today.getFullYear() - todate.getFullYear();
		}
	}
}
				
function getLeftDays(todate) {
	var today = new Date();
	if (today.getTime() > todate.getTime()) {
		if (today.getMonth() > todate.getMonth() || 
			(today.getMonth() == todate.getMonth() && today.getDate() > todate.getDate())) {
			todate.setFullYear(today.getFullYear() + 1);
		} else {
			todate.setFullYear(today.getFullYear());
		}
	}
	return Math.ceil((todate.getTime()-today.getTime()) / one_day);
}

function onTimer() {
	var date = new Date();
	if (date.getHours() == 0 && date.getMinutes() == 0) {
		refreshTables();
	}
}

function validateDate(year, month, date) {
	if (year != "" && year.match(year_pattern) == null) {
		alert("年份输入有误");
		return false;
	}
	if (month.match(month_pattern) == null) {
		alert("月份输入有误");
		return false;
	}
	if (date.match(date_pattern) == null) {
		alert("日份输入有误");
		return false;
	}
	if (year == "") {
		var d = new Date(2001, month - 1, date);
		if ((d.getMonth() + 1) != month || d.getDate() != date) {
			alert("日期输入有误");
			return false;
		} 
	} else {
		var d = new Date(year, month - 1, date);
		if (d.getFullYear() != year || (d.getMonth() + 1) != month || d.getDate() != date) {
			alert("日期输入有误");
			return false;
		}
	}
	return true;
}
				
init();